package com.skycong.hello77.activity.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.os.StrictMode;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import com.skycong.hello77.R;
import com.skycong.hello77.activity.toast.ToastActivity;
import com.skycong.hello77.datepicker.TopAndBottomPop;
import com.skycong.hello77.datepicker.entity.OwnTypeSelect;
import com.skycong.skycongtool.Http;

import java.io.IOException;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener,TopAndBottomPop.onTopAndBottomPopLister {

    private Context context = DialogActivity.this;
    private List<OwnTypeSelect> ownTypeSelects = new ArrayList<>();
    private TopAndBottomPop pop;
    private Button free_dialog;
    private  Button comfit_dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //强制在住Activity中执行网络操作
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.activity_dialog);
        comfit_dialog = (Button) findViewById(R.id.comfit_dialog);
        comfit_dialog.setOnClickListener(this);
        Button single_dialog = (Button) findViewById(R.id.single_dialog);
        single_dialog.setOnClickListener(this);
        Button list_dialog = (Button) findViewById(R.id.list_dialog);
        list_dialog.setOnClickListener(this);
        Button multiply_dialog = (Button) findViewById(R.id.multiply_dialog);
        multiply_dialog.setOnClickListener(this);
         free_dialog = (Button) findViewById(R.id.free_dialog);
        free_dialog.setOnClickListener(this);
        setData();
        pop = new TopAndBottomPop(context,ownTypeSelects,this);
    }

    //设置弹出窗里的数据
    private void setData() {
        ownTypeSelects.add(new OwnTypeSelect(1,"选择一",true));
        ownTypeSelects.add(new OwnTypeSelect(2,"选择二",false));
        ownTypeSelects.add(new OwnTypeSelect(3,"选择三",false));
        ownTypeSelects.add(new OwnTypeSelect(4,"选择四",false));
        ownTypeSelects.add(new OwnTypeSelect(5,"选择五",false));
        ownTypeSelects.add(new OwnTypeSelect(6,"选择六",false));
        ownTypeSelects.add(new OwnTypeSelect(7,"选择七",false));
    }

    @Override
    public void onClick(View v) {
        int butId = v.getId();

        switch (butId) {
            //确定dialog
            case R.id.comfit_dialog: {
                //再点击的位置下面显示弹出窗
                pop.setPopShow(comfit_dialog);
                //得到builder
//                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
//                builder.setTitle("确认对话框请求百度");
//                builder.setMessage("确认弹框提示内容");
//                builder.setIcon(R.mipmap.hahaha);
//                //设置确认按钮
//                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        try {
//                            String s = Http.get("http://www.baidu.com");
//                            Toast.makeText(context, s, 0).show();
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//                //设置取消按钮
//                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(context, "你点击了取消", 0).show();
//
//                    }
//                });
//                //创建对话框
//                AlertDialog dialog = builder.create();
//                dialog.show();

                break;
            }
            //单选dialog
            case R.id.single_dialog: {
                final String[] data = {"程序员", "C++", "Java", "Long"};
                //得到builder
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("单选按钮对话框");
                builder.setIcon(R.mipmap.hahaha);
                //设置单选按钮 初始化数据，默认选中位置
                builder.setSingleChoiceItems(data, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了" + data[which], 0).show();
                    }
                });
                //设置取消按钮
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了取消", 0).show();
                        //取消弹框
                        dialog.dismiss();

                    }
                });
                //创建对话框
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            }
            //列表dialog
            case R.id.list_dialog: {
                final String[] data = {"程序员", "C++", "Java", "Long", "策划"};
                //得到builder
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("列表对话框");
                builder.setIcon(R.mipmap.hahaha);
                //设置单选按钮 初始化数据，默认选中位置
                builder.setItems(data, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了" + data[which], 0).show();
                    }
                });
                //设置取消按钮
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了取消", 0).show();
                        //取消弹框
                        dialog.dismiss();

                    }
                });
                //创建对话框
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            }
            //多选dialog
            case R.id.multiply_dialog: {
                final String[] data = {"程序员", "C++", "Java", "Long", "int", "Class"};
                //得到builder
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("多选按钮对话框");
                builder.setIcon(R.mipmap.hahaha);
                //设置单选按钮 初始化数据，默认选中位置
                builder.setMultiChoiceItems(data, new boolean[]{true, true, false, false, true, false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(context, "你点击了" + data[which], 100).show();
                        } else {
                            Toast.makeText(context, "你取消了" + data[which], 100).show();
                        }
                    }
                });
                //设置确认按钮
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了确认", 0).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
                //创建对话框
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            }
            //自定义dialog
            case R.id.free_dialog: {
                //得到builder
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                LayoutInflater inflater = LayoutInflater.from(context);
                View dialog_layout = inflater.inflate(R.layout.dialog_layout, null);
                builder.setView(dialog_layout);
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            }
        }
    }

    @Override
    public void onDismiss() {

    }

    @Override
    public void onReciverItem(OwnTypeSelect ownTypeSelect, int position) {
         for(int  i=0;i<ownTypeSelects.size();i++){
             if(i==position){
                 ownTypeSelects.get(i).isDefault = true;
             }else{
                 ownTypeSelects.get(i).isDefault = false;
             }
         }
        free_dialog.setText(ownTypeSelect.name);
    }
}
